package br.unicentro.lynx.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.unicentro.lynx.model.factory.ConnectionFactory;
import br.unicentro.lynx.model.pojo.Bookcase;

public class BookcasesDao {
	private String sql;
	private Connection connection;
	private PreparedStatement statement;
	private ResultSet result;

	public void insert(Bookcase bookcase) {
		open();
		this.sql = "INSERT INTO Bookcases (description_bookcase) VALUES (?)";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setString(1, bookcase.getDescriptionBookcase());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}

	}

	public void update(Bookcase bookcase) {
		open();
		this.sql = "UPDATE Bookcases SET description_bookcase = ? WHERE id_bookcase = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setString(1, bookcase.getDescriptionBookcase());
			this.statement.setInt(2, bookcase.getIdBookcase());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void delete(Bookcase bookcase) {
		open();
		this.sql = "DELETE FROM Bookcases WHERE id_bookcase = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, bookcase.getIdBookcase());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public Bookcase search(Integer id_bookcase) {
		open();
		this.sql = "SELECT * FROM Bookcases WHERE id_bookcase = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, id_bookcase);
			this.result = this.statement.executeQuery();
			if (this.result.next()) {
				return new Bookcase(result.getInt("id_bookscase"),
						result.getString("description_bookcase"));
			} else {
				return null;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public ArrayList<Bookcase> list() {
		open();
		this.sql = "SELECT * FROM Bookcases";
		ArrayList<Bookcase> areas = new ArrayList<Bookcase>();
		try {
			this.statement = connection.prepareStatement(sql);
			this.result = this.statement.executeQuery();
			while (this.result.next()) {
				areas.add(new Bookcase(result.getInt("id_bookcase"), result
						.getString("description_bookcase")));
			}
			return areas;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				this.result.close();
				close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}

	private void open() {
		this.connection = ConnectionFactory.getConnection(
				ConnectionFactory.URL, ConnectionFactory.USER,
				ConnectionFactory.PASSWORD);
	}

	private void close() {
		try {
			this.statement.close();
			this.connection.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
